package com.societyGames.codeAlong.state.codeExercise.model
{
	public class SyntaxErrorMessage
	{
		private var _identifier:String;
		private var _line:int;
		private var _message:String;
		private var _syntaxError:SyntaxError;
		
		public function get identifier():String			
		{
			return this._identifier;
		}
		
		public function set line(value:int):void
		{
			this._line = value;
		}
		
		public function get line():int
		{
			return this._line;
		}
		
		//Gets only the message part.
		public function get message():String
		{
			return this._message;			
		}
		
		//Returns the Syntax error as an entire string.
		public function toString():String
		{
			return this._identifier + ":" + (this._line + 1).toString() + ":" + this._message;
		}
		
		public function get syntaxError():SyntaxError
		{
			return this._syntaxError;
		}
		
		public static function fromSyntaxError(syntaxError:SyntaxError):SyntaxErrorMessage
		{
			var text:String = syntaxError.message;
			//main:1: Expected identifier, found semicolon
			var parts:Vector.<String> = new Vector.<String>();
			for (var index:int = 0; index < 3; index++)
			{
				if (index != 2)
				{
					var indexOf:int = text.indexOf(":"); 
					if (indexOf != -1)
					{
						parts.push(text.slice(0, indexOf));
						text = text.slice(indexOf + 1); //Remove the ":"
					}
					else
					{
						parts.push("");
					}
				}
				else
				{
					parts.push(text);					
				}
			}
			var syntaxErrorMessage:SyntaxErrorMessage = new SyntaxErrorMessage();
			syntaxErrorMessage._identifier = parts[0];
			syntaxErrorMessage._line = int(parts[1]) - 1;
			syntaxErrorMessage._message = parts[2];
			syntaxErrorMessage._syntaxError = syntaxError;
			return syntaxErrorMessage;
		}
	}
}